package ru.neoflex.datatable;

import java.awt.event.ActionListener;

import javax.management.Descriptor;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import ru.neoflex.dataobject.DataObjectDescriptor;
import ru.neoflex.dataobject.DataObjectProperty;
import ru.neoflex.datatable.renders.ComboboxRenderer;
import ru.neoflex.datatable.renders.PaddedComboboxRenderer;

public class TableColumnBuilderImpl  implements TableColumnBuilder {
	private final DataObjectDescriptor descriptor;
	
	public TableColumnBuilderImpl(DataObjectDescriptor descriptor) {
		this.descriptor = descriptor;
	}

	private int getColumnIndex(String columnName) {
		for (int i = 0; i < descriptor.getProperties().size(); i++) {
			DataObjectProperty property = descriptor.getProperties().get(i);
			if (property.getName().equals(columnName)) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public TableColumn getNumericTableColumn(String columnName, String header, String format,
			ColumnStyle style) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TableColumn getStringTableColumn(String columnName, String header, String format,
			ColumnStyle style) {
		int columnIndex = getColumnIndex(columnName);
		TableColumn tableColumn = new TableColumn(columnIndex);
		tableColumn.setHeaderValue(header);
		return tableColumn;
	}

	@Override
	public TableColumn getComboboxTableColumn(String columnName, String header, 
			ComboBoxModel model) {
		int columnIndex = getColumnIndex(columnName);
		TableColumn tableColumn = new TableColumn(columnIndex);
		tableColumn.setHeaderValue(header);
		tableColumn.setCellRenderer(new DefaultTableCellRenderer());
		tableColumn.setCellEditor(new DefaultCellEditor(new JComboBox(model)));
		return tableColumn;
	}

	@Override
	public TableColumn getActionTableColumn(String columnName, String header, 
			ActionListener listener) {
		// TODO Auto-generated method stub
		return null;
	}
}
